package com.example.eventreg.organizer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eventreg.security.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/organizer")
@CrossOrigin
@RestController // 用于定义该类为一个RESTful API控制器
public class OrganizerController {
    @Autowired
    private OrganizerMapper organizerMapper;
    @Autowired
    private JwtUtil jwtUtil;
    // 构造函数，用于初始化模拟数据
    public OrganizerController() {
        // 初始化一些用户数据

    }
    @GetMapping("/self")
    public Organizer getOrgSelf(@RequestHeader("Authorization") String token) {
        System.out.println("token: "+token);
        if (token != null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if(token.startsWith("Bearer "))
            {
                token = token.substring(7);
            }
            System.out.println("tokenC: "+token);
            if (jwtUtil.getTypeFromToken(token).equals("O"))
            {
                String oid = jwtUtil.getUserIdFromToken(token);
                return organizerMapper.selectOne(new QueryWrapper<Organizer>().eq("oid", oid));
            }
        }
        return null;
    }
    @GetMapping("/get")
    public List<Organizer> getAllOrg() {
        return organizerMapper.selectList(null); // 返回200状态码和用户列表
    }
    @GetMapping("/get/{id}")
    public List<Organizer> getOneOrg(@PathVariable String id) {
        return organizerMapper.selectList(null); // 返回200状态码和用户列表
    }
    //创建活动组织者
    @PostMapping("/reg")
    public Organizer createOrg(@RequestBody Organizer organizer) {
        organizerMapper.insert(organizer);
        return organizer;
    }
    //更新
    @PutMapping("/update")
    public int updateOrg(@RequestBody Organizer organizer) {
        return organizerMapper.updateById(organizer);
    }
    @PutMapping("/del/{oid}")
    public Integer deleteOrg(@PathVariable String oid) {
        return organizerMapper.deleteById(oid);
    }

    // 映射GET请求，获取所有用户
    /*
    //创建新用户
    @PostMapping("/users")
    public Student createUser(@RequestBody Student student) {
        studentMapper.insert(student);
        return student;
    }
    // 更新指定ID的用户
    @PutMapping("/users/{id}")
    public Integer updateUser(@PathVariable Long id, @RequestBody Student updatedStudent) {
        return studentMapper.updateById(updatedStudent);
    }
    // 禁用指定ID的用户
    @PutMapping("/disable/{id}")
    public String disableUser(@PathVariable Long id) {
        // 创建UpdateWrapper来设置更新条件
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 条件：ID匹配
        updateWrapper.set("status", "DISABLED"); // 更新status为DISABLED
        // 执行更新操作
        boolean updated = studentMapper.update(null, updateWrapper) > 0;
        // 返回禁用成功的消息
        if (updated) {
            return "用户ID【 " + id + " 】已禁用";
        } else {
            return "禁用用户失败，用户ID【 " + id + " 】不存在或更新失败";
        }
    }*/

}